package com.betty.cscams.service.impl;

import com.betty.cscams.common.page.MybatisPageHelper;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.common.page.PageResult;
import com.betty.cscams.dao.WarningMapper;
import com.betty.cscams.entity.Warning;
import com.betty.cscams.service.WarningService;
import com.betty.cscams.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * ---------------------------
 *  智能预警信息Service实现类(WarningServiceImpl)         
 * ---------------------------
 */
@Service
public class WarningServiceImpl implements WarningService {

    @Autowired
    private WarningMapper warningMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(Warning record) {
        record.setLastUpdateBy(SecurityUtils.getUsername());
        record.setLastUpdateTime(new Date());
        if(record.getId() == null || record.getId() == 0) {
            // 新增记录
            record.setCreateBy(SecurityUtils.getUsername());
            record.setCreateTime(new Date());
            record.setDelFlag(0);
            return warningMapper.add(record);
        }
        return warningMapper.update(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Warning record) {
        return warningMapper.delete(record.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(List<Warning> records) {
        for(Warning record : records) {
            delete(record);
        }
        return 1;
    }

    @Override
    public Warning findById(Long id) {
        return warningMapper.findById(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        Object studentNum = pageRequest.getParam("studentNum");
        Object status = pageRequest.getParam("status");
        return MybatisPageHelper.findPage(pageRequest, warningMapper, "findPage", 
                studentNum, status);
    }

    @Override
    public int countUnreadByStudentNum(String studentNum) {
        return warningMapper.countUnreadByStudentNum(studentNum);
    }

    @Override
    public Warning findPsyWarningByStudentAndTerm(String studentNum, Long termId) {
        return warningMapper.findPsyWarningByStudentAndTerm(studentNum, termId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int markAsRead(Long id) {
        return warningMapper.markAsRead(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int markAllAsRead(String studentNum) {
        return warningMapper.markAllAsRead(studentNum);
    }

    @Override
    public List<Warning> findCurrentUserWarnings(Integer status) {
        String studentNum = SecurityUtils.getUsername();
        return warningMapper.findPage(studentNum, status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createPsyWarning(String studentNum, Long termId, int score) {
        // 检查是否已有预警
        Warning existWarning = findPsyWarningByStudentAndTerm(studentNum, termId);
        if(existWarning != null) {
            return false; // 已有预警，不重复创建
        }
        
        // 创建新预警
        Warning warning = new Warning();
        warning.setStudentNum(studentNum);
        warning.setTermId(termId);
        warning.setType(1); // 心理测评预警
        warning.setContent("您的心理测评得分为" + score + "分，低于60分，请及时关注自己的心理健康状况。");
        warning.setStatus(0); // 未读
        warning.setCreateBy("system");
        warning.setCreateTime(new Date());
        warning.setLastUpdateBy("system");
        warning.setLastUpdateTime(new Date());
        warning.setDelFlag(0);
        
        return warningMapper.add(warning) > 0;
    }
} 